package archer.application.core.service.client;

import archer.application.core.constant.Constant;
import archer.application.core.entity.user.Client;
import archer.application.core.service.user.UserService;
import archer.application.core.service.BaseService;
import archer.framework.protocol.result.ExecuteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 前端用户Service
 *
 * @author christ
 * @date 2016/6/7
 */
@Component
public class ClientService extends BaseService {

    private static String type = Constant.UserType.CLIENT;

    @Autowired
    UserService userService;

    /**
     * 手机号是否已注册
     *
     * @param mobile
     * @return
     */
    public ExecuteResult mobileRegistered(String mobile) {

        // 代理给userService进行验证
        return userService.mobileRegistered(mobile, type);
    }

    /**
     * 用手机号进行注册
     *
     * @param mobile
     * @param password
     * @return
     */
    public ExecuteResult register(String mobile, String password) {

        if (isEmpty(mobile)) {
            throwParamNullException("手机号不能为空");
        }

        if (isNotMoblie(mobile)) {
            throwParamInvalidateException("手机号格式不正确");
        }

        if (isEmpty(password)) {
            throwParamNullException("密码不能为空");
        }

        if (mobileRegistered(mobile).isTrue()) {
            throwParamInvalidateException("该手机号已注册");
        }

        new Client().setMobile(mobile).setPassword(password).store();

        return success();
    }

    /**
     * 登录
     *
     * @param mobile
     * @param password
     * @return
     */
    public ExecuteResult login(String mobile, String password) {

        if (isEmpty(mobile)) {
            throwParamNullException("手机号不能为空");
        }

        if (isNotMoblie(mobile)) {
            throwParamInvalidateException("手机号格式不正确");
        }

        if (isEmpty(password)) {
            throwParamNullException("密码不能为空");
        }

        return userService.mobileLogin(mobile, password, type);
    }

    /**
     * 退出
     *
     * @return
     */
    public ExecuteResult logout() {

        return userService.logout();
    }
}
